package controllers

import (
	"encoding/json"
	"io"
	"log"
	"net/http"
	"xmgl/models"

	"github.com/astaxie/beego"
	//"strconv"
)

type LyfjController struct {
	beego.Controller
}

type GetLyController struct {
	beego.Controller
}

type GetFjController struct {
	beego.Controller
}

type GetLyfjController struct {
	beego.Controller
}

//房间验证的API，用于POST,beego.Router("/api/v1/fjValidator", &controllers.Api_fjController{})
type Api_fjController struct {
	beego.Controller
}

func (this *LyfjController) Get() {
	var (
		menu models.Menu_gl
		user models.Sut_user
	)

	if this.GetSession("USER") == nil {
		//没有取到session值，页面跳转
		http.Redirect(this.Ctx.ResponseWriter, this.Ctx.Request, "/login", http.StatusFound)
	}

	tmp, ok := this.GetSession("USER").(models.Sut_user) //注意类型断言
	if ok {
		user = tmp
	}
	log.Println("Lyfj.go MainController Get() 从session中取出用户姓名：", user.User_name) //good

	this.Data["WEBSITE"] = "项目管理-楼宇房间"
	this.Data["MENU_LEFT"] = menu.Get_left_menu()
	this.Data["MENU_RIGHT"] = menu.Get_right_menu()
	this.Data["USER_NAME"] = user.User_name

	this.Layout = "layout.html"
	this.TplName = "lyfj.html"
	this.LayoutSections = make(map[string]string)
	this.LayoutSections["HtmlHeader"] = "html_header.html"
}

//url: '/api/v1/fj?pid=' + pid，返回json数组
func (this *GetFjController) Get() {
	var (
		ts        []models.Sut_lyfj
		parent_id int
		err       error
		json1     []byte
	)
	type xx struct {
		Total int
		Rows  int
	}
	parent_id, err = this.GetInt("pid")
	if err != nil {
		return
	}
	ts = models.Get_lyfjs(1, parent_id) //项目ID号1
	if len(ts) > 0 {
		json1, err = json.MarshalIndent(ts, "", "")
		if err != nil {
			return
		}
		io.WriteString(this.Ctx.ResponseWriter, string(json1[:]))
	} else {
		io.WriteString(this.Ctx.ResponseWriter, "[]")
	}
	//log.Println("GetFjController，json_data:", string(json1))
}

//url: '/api/v1/ly?pid=' + pid，返回的是父子关系的楼宇及楼层的json数据，无房间数据
func (this *GetLyController) Get() {
	var (
		json1 []byte
		//parent_id int
		err error
	)
	type xx struct {
		Total int
		Rows  int
	}
	//parent_id, err = this.GetInt("pid")
	//log.Println("/api/v1/ly	, pid:", parent_id, "\t err:", err)
	if err != nil {
		//没有取到PID值，取出所有项目对应的楼宇列表
		json1 = models.Get_lyfj_json(1, 5) //项目ID号1
		//log.Println("json_data:", string(json1))
	} else {

	}
	//ts = models.Get_lyfjs()	//父子结构的数据
	//json1, err = json.MarshalIndent(ts, "", "")
	/*
		if err != nil {
			return
		}
	*/
	io.WriteString(this.Ctx.ResponseWriter, string(json1[:]))
}

//url:/api/v1/lyfj,返回楼宇及房间的树形父子关系json数据
func (this *GetLyfjController) Get() {
	var (
		json1 []byte
	)
	type xx struct {
		Total int
		Rows  int
	}

	//取出所有项目对应的楼宇列表
	json1 = models.Get_lyfj_json(1, 6) //项目ID号1，6表示所有楼宇及房间
	//log.Println("json_data:", string(json1))

	//ts = models.Get_lyfjs()	//父子结构的数据
	//json1, err = json.MarshalIndent(ts, "", "")
	/*
		if err != nil {
			return
		}
	*/
	io.WriteString(this.Ctx.ResponseWriter, string(json1[:]))
}

func (this *Api_fjController) Post() {
	var (
		//err error
		fj   models.Sut_fj
		rets models.Sut_validator
	)
	fj.Name = this.GetString("jsmc", "")
	log.Println("this.Ctx.Input.Param('jsmc'):", fj.Name)
	if fj.Name == "" {
		//提交的没有ID号这个字段，则返回所有工单
	} else {
		//取到了ID号，则只返回这个ID号的工单
		rets.Valid = fj.Validator_fj_name()
		if rets.Valid == true {
		} else {
			rets.Message = "没有这个教室！"
		}

	}
	this.Data["json"] = rets
	this.ServeJSON()
}

//房间验证的API，用于POST,beego.Router("/api/v1/fjValidator", &controllers.Api_fjController{})
func (this *Api_fjController) Get() {
	var (
		//err error
		fj   models.Sut_fj
		rets models.Sut_validator
	)
	fj.Name = this.GetString("jsmc", "")
	log.Println("this.Ctx.Input.Param('jsmc'):", fj.Name)
	if fj.Name == "" {
		//提交的没有ID号这个字段，则返回所有工单
	} else {
		//取到了ID号，则只返回这个ID号的工单
		rets.Valid = fj.Validator_fj_name()
		if rets.Valid == true {
		} else {
			rets.Message = "没有这个教室！"
		}

	}
	this.Data["json"] = rets
	this.ServeJSON()
}
